//
//  RCDSearchHeaderView.m
//  jsy
//
//  Created by  任辰东 on 2020/11/12.
//  Copyright © 2020 JSY. All rights reserved.
//

#import "RCDSearchHeaderView.h"
#import "UIButton+TouchAreaInsets.h"

@interface RCDSearchHeaderView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UITextField *searchTF;
@property (nonatomic, strong) UIImageView *searchIV;
@property (nonatomic, strong) UIButton *searchBtn;
@end

@implementation RCDSearchHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupViews];
        [self setupLayout];
    }
    return self;
}

- (void)setupViews
{
    self.backgroundColor = UIColorFromHex(kColorBG);
    [self addBottomLine];
    
    self.bgView = [UIView new];
    self.bgView.backgroundColor = UIColorFromHex(0xE8E8E8);
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 4;
    [self addSubview:self.bgView];
    
    self.searchIV = [[UIImageView alloc]initWithImage:UIImageWithName(@"gathering_ search")];
    [self.bgView addSubview:self.searchIV];
    
    self.searchTF = [UITextField new];
    self.searchTF.placeholder = @"请输入关键词";
    self.searchTF.textColor = UIColorFromHex(0x999999);
    self.searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.searchTF.returnKeyType = UIReturnKeySearch;
    self.searchTF.font = UIFontSize(16);
    self.searchTF.delegate = self;
    self.searchTF.enablesReturnKeyAutomatically = YES;
    self.searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.bgView addSubview:self.searchTF];
    
    [self addSubview:self.searchBtn];
    
    [self.searchTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupLayout
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(kPaddingTop);
        make.left.equalTo(self).offset(kPaddingLeft);
        make.right.equalTo(self.searchBtn.mas_left).offset(kPaddingRight);
        make.bottom.equalTo(self).offset(kPaddingBottom);
        make.height.mas_equalTo(35);
    }];
    
    [self.searchIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.bgView).offset(10);
        make.bottom.equalTo(self.bgView).offset(-10);
        make.left.equalTo(self.bgView).offset(16);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.bgView);
        make.left.equalTo(self.searchIV.mas_right).offset(7);
        make.right.equalTo(self.bgView.mas_right).offset(0);
    }];
    
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self).offset(kPaddingTop);
        make.bottom.equalTo(self).offset(kPaddingBottom);
        make.right.equalTo(self).offset(kPaddingRight);
        make.width.mas_equalTo(70);
        
    }];
    self.searchBtn.layer.cornerRadius = 5;
    self.searchBtn.layer.masksToBounds = YES;
}

- (void)searchBtnAction
{
    if (self.searchBtnCallBack) {
        self.searchBtnCallBack(self.searchTF.text);
    }
}

-(void)textFieldDidChange :(UITextField *)theTextField{
    
    if (self.textFieldCallBack) {
        self.textFieldCallBack(theTextField.text);
    }
}

#pragma mark --UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (self.textFieldReturnCallBack) {
        self.textFieldReturnCallBack(textField.text);
    }
    return YES;
}

- (void)setFirstResponder
{
    [self.searchTF becomeFirstResponder];
}

- (void)removeFirstResponder
{
    if (self.textFieldCallBack) {
        self.textFieldCallBack(self.searchTF.text);
    }
    [self.searchTF resignFirstResponder];
}

-(UIButton *)searchBtn
{
    if (!_searchBtn) {
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        _searchBtn.titleLabel.font = UIFontSize(16);
        [_searchBtn setTitleColor:UIColorFromHex(kColorWhite) forState:UIControlStateNormal];
        [_searchBtn setBackgroundColor:UIColorFromHex(kColorMain)];
        [_searchBtn setTouchAreaInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    }
    return _searchBtn;
}


@end
